package com.coder.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.coder.entity.CollSat;
import com.coder.mapper.CollSatMapper;
import com.coder.service.CollSatService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 王令
 */
@Service
public class CollSatServiceImpl extends ServiceImpl<CollSatMapper, CollSat> implements CollSatService {
    @Override
    public Map<String, Object> collSatInfo() {
        Map<String, Object> map = new HashMap<>();
        List<CollSat> collect = list(new QueryWrapper<CollSat>().select("distinct address"));
        List<String> address = new ArrayList<>();
        collect.forEach(v -> {
            address.add(v.getAddress());
        });
        map.put("address", address);
        return map;
    }

    @Override
    public IPage<CollSat> pages(Page<CollSat> page, CollSat collSat) {
        return page(page, new QueryWrapper<CollSat>()
                .select("colleges", "address", "complex_sat", "env_sat", "life_sat")
                .like(StrUtil.isNotBlank(collSat.getColleges()), "colleges", collSat.getColleges())
                .like(StrUtil.isNotBlank(collSat.getAddress()), "address", collSat.getAddress())
                .orderBy(StrUtil.isNotBlank(collSat.getOrderProp()) && StrUtil.isNotBlank(collSat.getOrder()),
                        StrUtil.equals("ascending", collSat.getOrder()),
                        StrUtil.isNotBlank(collSat.getOrderProp())? collSat.getOrderProp().replace("S", "_s"): "complex_sat")
        );
    }
}
